package com.shopmall.controller;

import com.shopmall.pojo.Admin;
import com.shopmall.pojo.AdminRecord;
import com.shopmall.service.AdminRecordServiceImpl;
import com.shopmall.service.AdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;

@Controller
public class SelfController {

    @Autowired
    private AdminServiceImpl adminService;

    @Autowired
    private AdminRecordServiceImpl adminRecordService;

    //self_show跳转在 AdminController 中

    @RequestMapping("/self/account_check")
    public String account_check(){
        return "self/account_check";
    }

    @RequestMapping("/self/account_check_do")
    public String account_check_do(@RequestParam String password, HttpSession session, Model model){
        Admin admin = (Admin) session.getAttribute("admin");
        String username = admin.getUsername();
        Admin admin_real=adminService.queryAdminByUsername(username);
        if(admin_real.getPassword().equals(password))
            return "/self/modify_password";
        else{
            model.addAttribute("msg","验证失败！你没有权限修改密码！");
            return "home";
        }
    }

    @RequestMapping("/self/modify_password_do")
    public String modify_password_do(@RequestParam String password1, @RequestParam String password2,
                                     Model model, HttpSession session){

        Timestamp date = new Timestamp(System.currentTimeMillis());
        Admin account = (Admin) session.getAttribute("admin");
        AdminRecord record = new AdminRecord();
        record.setDo_who(account.getUsername());
        record.setDo_time(date);
        int state = account.getState();
        String string1;
        if(state==0){
            string1="超级管理员";
        }
        else if(state==1){
            string1="系统管理员";
        }
        else {
            string1="商品管理员";
        }
        record.setDo_role(string1);
        record.setDone_who("个人信息");
        record.setDone_role("管理员本人");
        record.setDo_what("修改本人密码");
        //以上是记录信息设置

        Admin admin = (Admin) session.getAttribute("admin");
        String username=admin.getUsername();
        Admin admin1 = new Admin();
        admin1.setUsername(username);
        admin1.setPassword(password1);
        if(password1.equals(password2)){
            adminService.modify_password(admin1);
            adminRecordService.addRecord(record);
            model.addAttribute("msg","修改密码成功！请重新登录！");
            return "redirect:/exit";
        }
        else{
            model.addAttribute("msg","两次输入的密码不一致，修改失败！");
            return "redirect:/self/account_check";
        }
    }

    @RequestMapping("/self/modify_self")
    public String modify_self(HttpSession session,Model model){
        Admin admin = (Admin)session.getAttribute("admin");
        int id=admin.getAdmin_id();
        Admin self = adminService.queryAdminById(id);
        model.addAttribute("self",self);
        return "/self/modify_self";
    }

    @RequestMapping("/self/modify_self_do")
    public String modify_self_do(Admin self,HttpSession session,Model model){

        Timestamp date = new Timestamp(System.currentTimeMillis());
        Admin account = (Admin) session.getAttribute("admin");
        AdminRecord record = new AdminRecord();
        record.setDo_who(account.getUsername());
        record.setDo_time(date);
        int state = account.getState();
        String string1;
        if(state==0){
            string1="超级管理员";
        }
        else if(state==1){
            string1="系统管理员";
        }
        else {
            string1="商品管理员";
        }
        record.setDo_role(string1);
        record.setDone_who("个人信息");
        record.setDone_role("管理员本人");
        record.setDo_what("修改个人信息");
        //以上是记录信息设置

        self.setUpdate_date(date);
        adminService.updateAdmin(self);
        adminRecordService.addRecord(record);
        model.addAttribute("msg","成功修改个人信息！");
        return "/home";
    }

}
